#
# @lc app=leetcode.cn id=2874 lang=python3
#
# [2874] 有序三元组中的最大值 II
#

# @lc code=start
from typing import List


class Solution:
    def maximumTripletValue(self, nums: List[int]) -> int:
        if min(nums) < 0:
            return 0
        gap = max(0,nums[0] - nums[1]) #记录最大差值
        mx = max(nums[0],nums[1]) #记录遍历位置之前的最大值
        ans = 0
        n = len(nums)
        for i in range(2,n):
            ans = max(ans,gap*nums[i])
            gap = max(gap,mx-nums[i])
            mx = max(mx,nums[i])
        return ans

# @lc code=end

